package com.fingard.dsp.bank.directbank.cmb02;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.IAcceptNotice;
import com.fingard.dsp.bank.directbank.cmb02.util.SignUtil;
import com.fingard.generalobj.NoticeReq;
import com.fingard.generalobj.NoticeResp;
import com.fingard.io.FileHelper;
import com.fingard.net.listener.SocketReceiver;

import java.io.File;
import java.util.Date;

public class CMB02AcceptNotice extends CMB02Base implements IAcceptNotice {
    @Override
    public NoticeResp dealNotice(NoticeReq pNtReq) throws Exception {
        NoticeResp resp = new NoticeResp();
        String p_reqs = pNtReq.getReqString();
        WriteBankLogLn("接收到请求：" + pNtReq.getFullString());

        String tmpStrRet = "";

        try{
            String authStatus = p_reqs.substring(208,209);
            if ("A".equalsIgnoreCase(authStatus)) {
                authStatus = "success";
            } else {
                authStatus = "fail";
            }

            StringBuilder sbMsg = new StringBuilder();
            sbMsg.append("<ATSYH>");
            sbMsg.append("<TransResp>");
            sbMsg.append("<BankCode>CMB02</BankCode>");
            sbMsg.append("<TransType>ContractNotify</TransType>");
            sbMsg.append("<TransSeqID>"+getQryTransID()+"</TransSeqID>");
            sbMsg.append("<TransParam>");
            sbMsg.append("<DetailRecord>");
            sbMsg.append("<RespCode>0</RespCode>");
            sbMsg.append("<RespInfo></RespInfo>");
            sbMsg.append("<ReqSeqID></ReqSeqID>");
            sbMsg.append("<PayAct>"+p_reqs.substring(60,80)+"</PayAct>");
            sbMsg.append("<SignState>"+authStatus+"</SignState>");
            sbMsg.append("<AuthCode>"+p_reqs.substring(0,20)+"</AuthCode>");
            sbMsg.append("</DetailRecord>");
            sbMsg.append("</TransParam>");
            sbMsg.append("</TransResp>");
            sbMsg.append("</ATSYH>");

            String tmpMacKey = "";
            String tmpMacKeyPath = getCfgDirectory() + File.separator + "macKey";
            if (!new File(tmpMacKeyPath).exists()) {
                String[] tmpMacKeyRet = exchangeMacKey();
                if (tmpMacKeyRet[0].length() != 0) {
                    WriteBankLogLn("密钥文件获取失败："+tmpMacKeyRet[1]);
                } else {
                    tmpMacKey = FileHelper.readFileText(tmpMacKeyPath, getCharset());
                }
            } else {
                tmpMacKey = FileHelper.readFileText(tmpMacKeyPath, getCharset());
            }
            WriteBankLogLn("macKey:" + tmpMacKey);

            StringBuilder tmpEachRecordReq = new StringBuilder();

            StringBuilder tmpEachRecordBody = new StringBuilder();
            tmpEachRecordBody.append(String.format("%-7s", "CMBMB99"));
            tmpEachRecordBody.append(String.format("%-20s", p_reqs.substring(0,20)));

            StringBuilder tmpEachRecordHeader = new StringBuilder();
            tmpEachRecordHeader.append("CMBA");
            tmpEachRecordHeader.append(String.format("%-4s", tmpEachRecordBody.toString().getBytes(getCharset()).length));
            tmpEachRecordHeader.append("BKQD");
            tmpEachRecordHeader.append(String.format("%-8s", ""));
            tmpEachRecordHeader.append(String.format("%-6s", ""));
            tmpEachRecordHeader.append(String.format("%-30s", ""));
            Date tmpBgnDate = new Date();
            tmpEachRecordHeader.append(Format.Date8Format.format(tmpBgnDate));
            tmpEachRecordHeader.append(Format.Time6Format.format(tmpBgnDate));
            tmpEachRecordHeader.append(String.format("%-25s", getQryTransID()));
            tmpEachRecordHeader.append("0.2.0");
            tmpEachRecordHeader.append(String.format("%-10s", bankFront.corpNo));
            tmpEachRecordHeader.append(String.format("%-16s", SignUtil.ansi99(tmpMacKey, tmpEachRecordBody.toString())));

            tmpEachRecordReq.append(tmpEachRecordHeader).append(tmpEachRecordBody);

            tmpStrRet = tmpEachRecordReq.toString();
        }catch(Exception e){
            WriteBankLogLn(e);
        }

        WriteBankLogLn("响应："+tmpStrRet);
        resp.respBytes = tmpStrRet.getBytes(pNtReq.reqCharset);
        return resp;
    }

    @Override
    public void getContentLength(SocketReceiver p_receiver) throws Exception {
        if (p_receiver.contentLength == -1) {
            if (p_receiver.readedLength >= 126) {
                p_receiver.contentLength = Integer.parseInt(new String(p_receiver.reqBytes, 4, 4, getCharset()).trim());
                p_receiver.contentStartIndex = 126;
            }
        }
    }
}